<?php
/*
  功能： 广告表 model
  作者：cxm
  时间：2019-10-23
*/

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Adv extends BaseModel {
    use SoftDeletes;
    protected $dates = ['deleted_at']; //开启deleted_at
    protected $table='adv';  //绑定表
    protected $appends = ['url1'];
    /**
     * 广告位
     */
    public function position(){
        return $this->hasOne(AdvPosition::class,'id','position_id');
    }

    /**
     * 图片
     */
    public function file(){
        return $this->hasOne(File::class,'id','images');
    }

    /**
     * 检测对应的广告位是否存在广告
     * @param array $position_ids
     * @return mixed
     */
    public static function chooseIsAdv($position_ids=array()){
        if (!empty($position_ids)) {
            foreach ($position_ids as $item) {
                if (self::advNum($item) > 0) {
                    return true;
                    break;
                }
            }
        }
        return false;
    }

    /**
     * 根据广告位获取广告数量
     * @param $position_id
     * @return num
     */
    public static function advNum($position_id){
        return self::where('position_id',$position_id)->count();
    }

    /**
     * 路径差分
     */
    public function getUrl1Attribute(){
        if (!isset($this->attributes['url']) || empty($this->attributes['url'])) {
            return array();
        }
        $url = $this->attributes['url'];
        $data = parse_url($url);
        return $data;
    }
}